﻿
Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_Home_ProductNewsHome
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadData()
        End If
    End Sub

    Private Sub LoadData()
        Using Data As New VSEntities
            Dim p = (From q In Data.Categories Where q.MainCate = True Order By q.Sort Ascending Select q.CatId, q.Title, q.Description, q.QueryString, q.TitleEn).ToList
            rptCate.DataSource = p
            rptCate.DataBind()
            rptProductCate.DataSource = p
            rptProductCate.DataBind()
        End Using
    End Sub

    Protected Sub rptCate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCate.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim ltrTitle As Literal = e.Item.FindControl("ltrTitle")
        ltrTitle.Text = row.Title
        'Dim rptProduct As Repeater = CType(e.Item.FindControl("rptProduct"), Repeater)
        'Dim hplCate As HyperLink = CType(e.Item.FindControl("hplCate"), HyperLink)
        'Dim hidCateID As HiddenField = CType(e.Item.FindControl("hidCateID"), HiddenField)
        'Dim rptSubCate As Repeater = CType(e.Item.FindControl("rptSubCate"), Repeater)
        'Dim lblCate As Label = CType(e.Item.FindControl("lblCate"), Label)

        'Dim _CateID As Integer = row.CatId
        'lblCate.Text = "<h2>" & row.Title & "</h2>"
        'hplCate.NavigateUrl = MainDomain & "mua-ban/" & row.QueryString & "-" & row.CatId.ToString & ".html"
        'hplCate.Attributes.Add("style", "cursor:pointer")
        'Using Data As New VSEntities
        '    'Lay toan bo cap con cua Cate
        '    Dim a = (From r In Data.uspSelectSubCate(_CateID) Select r.CateID).ToArray

        '    Dim m = (From l In Data.Categories Where l.ParentId = _CateID Select l.Title, l.CatId, l.QueryString).Take(6).ToList

        '    rptSubCate.DataSource = m
        '    rptSubCate.DataBind()
        '    'Product
        '    Dim p = (From q In Data.Products Join
        '              h In Data.Categories On h.CatId Equals q.CateID
        '              Where (h.CatId = _CateID Or a.Contains(h.CatId)) _
        '              And q.StatusId = 3 And q.PublishedDate <= Now() _
        '              And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing)
        '              Order By q.PublishedDate Descending Select New With {q.ProductId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID, q.IsSellOff, q.ProductUnitId}).Take(8).ToList
        '    rptProduct.DataSource = p
        '    rptProduct.DataBind()
        'End Using
    End Sub
    Protected Sub rptProductCate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProductCate.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim imgCate As Image = e.Item.FindControl("imgCate")
        imgCate.ImageUrl = MainDomain & "Uploads/" & row.TitleEn & ".jpg"

        Dim lblTitleCate As Label = e.Item.FindControl("lblTitleCate")
        lblTitleCate.Text = row.Description()
        Dim rptProduct As Repeater = CType(e.Item.FindControl("rptProduct"), Repeater)
        Dim _CateID As Integer = row.CatId
        Using Data As New VSEntities
            'Lay toan bo cap con cua Cate
            Dim a = (From r In Data.uspSelectSubCate(_CateID) Select r.CateID).ToArray

            ' 4 product newest
            Dim p = (From q In Data.Products Join
                      h In Data.Categories On h.CatId Equals q.CateID
                      Where (h.CatId = _CateID Or a.Contains(h.CatId)) _
                      And q.StatusId = 3 And q.PublishedDate <= Now() _
                      And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing)
                      Order By q.PublishedDate Descending Select New With {q.ProductId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID, q.IsSellOff, q.ProductUnitId}).Take(8).ToList
            rptProduct.DataSource = p
            rptProduct.DataBind()

            ' Hot Keyword
            'Dim rptHotKeyWord As Repeater = CType(e.Item.FindControl("rptHotKeyWord"), Repeater)
            'Dim keyword = (From q In Data.Keywords Select q).Take(20).ToList
            'rptHotKeyWord.DataSource = keyword
            'rptHotKeyWord.DataBind()

            'Dim rptHotProduct As Repeater = CType(e.Item.FindControl("rptHotProduct"), Repeater)
            ''2 top hot
            'Dim hot = (From q In Data.Products Join
            '          h In Data.Categories On h.CatId Equals q.CateID
            '          Where (h.CatId = _CateID Or a.Contains(h.CatId)) _
            '          And q.StatusId = 3 And q.PublishedDate <= Now() _
            '          And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) _
            '          And q.IsHot = True _
            '          Order By q.PublishedDate Descending Select New With {q.ProductId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID, q.IsSellOff, q.ProductUnitId}).Take(2).ToList

            'rptHotProduct.DataSource = hot
            'rptHotProduct.DataBind()

        End Using



    End Sub


    Protected Sub rptProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row = CType(e.Item.DataItem, Object)
        Dim img As Image = CType(e.Item.FindControl("imgPreview"), Image)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        img.ImageUrl = CookedImageURL("160-180", row.ImagePreview)
        Dim lblTitle As Label = CType(e.Item.FindControl("lblTitle"), Label)
        img.AlternateText = row.Title
        img.Style.Add("overflow", "hidden")
        lblTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"

    End Sub

    Protected Sub rptSubCate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row = CType(e.Item.DataItem, Object)
        Dim hplSubCate As HyperLink = CType(e.Item.FindControl("hplSubCate"), HyperLink)
        hplSubCate.Text = row.Title
        hplSubCate.NavigateUrl = MainDomain & "mua-ban/" & row.QueryString & "-" & row.CatId.ToString & ".html"
    End Sub
    'Protected Sub rptHotKeyWord_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
    '    Dim row = CType(e.Item.DataItem, Keyword)
    '    Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
    '    hplTitle.Text = row.Keyword1
    '    hplTitle.NavigateUrl = MainDomain & "tu-khoa/" & row.QueryString & "-" & row.KeywordID.ToString & ".html"
    'End Sub
    Protected Sub rptHotProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row = CType(e.Item.DataItem, Object)
        Dim ltrTitle As Literal = CType(e.Item.FindControl("ltrTitle"), Literal)
        ltrTitle.Text = row.Title
        Dim img As Image = e.Item.FindControl("imgPreview")
        img.ImageUrl = CookedImageURL("120-120", row.ImagePreview)
    End Sub
    Protected Sub btnAddCart_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _ProductID As Integer
        _ProductID = sender.tooltip()
        AddShoppingCart(_ProductID)
        Session("LastURL") = Request.Url
        Response.Redirect(MainDomain & "gio-hang.html")


    End Sub

    Private Sub AddShoppingCart(ByVal _ProductID As String)

        Using Data As New VSEntities
            Dim _cartID As String = Session("CartId")
            Dim check = (From q In Data.ShoppingCarts Where q.CartId = _cartID And q.ProductId = _ProductID Select q).FirstOrDefault
            If check Is Nothing Then
                Dim p As New ShoppingCart
                p.CartId = _cartID
                p.ProductId = _ProductID
                p.UserId = Session("UserId")
                ' p.StoreId = (From q In Data.Products Where q.ProductId = _ProductID Select q.StoreId).FirstOrDefault
                p.Quantity = 1
                Data.ShoppingCarts.AddObject(p)
                Data.SaveChanges()

            End If
        End Using
    End Sub

End Class
